﻿package app.view.mediator
{	
	import adobe.utils.CustomActions;
    import app.facade.App; 			
	import app.facade.EApp;
	import app.model.proxy.ConnectProxy;
	import app.model.vo.Version;
	import app.view.component.Background;
	import app.view.component.BackShaded;
	import app.view.component.Datas;
	import app.view.component.FaceCore;
	import app.view.component.Game;
	import app.view.component.HumanRun;
	import app.view.component.Loading;
	import app.view.component.tooltips.ToolTip;
	import com.greensock.loading.LoaderMax;
	import com.hutra.utils.SWFProfiler;
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.ui.ContextMenu;
	import flash.ui.ContextMenuItem;
	import flash.utils.Dictionary;
	import pl.mateuszmackowiak.nativeANE.properties.SystemProperties;
	import so.cuo.platform.admob.*
    import flash.display.Stage;    
    import flash.display.StageScaleMode;    
    import flash.display.StageAlign;    
    
    import org.puremvc.as3.interfaces.*;
    import org.puremvc.as3.patterns.mediator.Mediator;
	
	import org.osflash.thunderbolt.Logger;
    /**
     * @author hungconcon
     */
    public class StageMediator extends FactoryMediator {
        public static const NAME:String = "StageMediator";	
		public var background:Background
		
        public function StageMediator( viewComponent:Object ){
            super( NAME, viewComponent );						
        }	
        override public function listNotificationInterests():Array  {
            return [
						App.SHOW_ERROR,
						App.INIT_APPLICATION,
						App.ASSET_LOAD_DONE
						
					];
        }
		override public function onRegister():void {
			super.onRegister();
			stage.align 	= StageAlign.TOP_LEFT;		
			stage.scaleMode = StageScaleMode.EXACT_FIT;
		}
        override public function handleNotification( note:INotification ):void {
			switch ( note.getName() )  {
				case App.INIT_LOAD_DATA_PROXY: 
					
				break;
				case App.SHOW_ERROR: break;
					trace("SHOW_ERROR: " + note.getBody());
				case App.INIT_APPLICATION:
					background 	= new Background();
					var loading:Loading 		= new Loading();
					var factory:Datas			= new Datas();
					
					
					stage.addChild(factory);
					stage.addChild(background);
					stage.addChild(loading);
					
					facade.registerMediator(new BackgroundMediator( background ));
					facade.registerMediator(new LoadingMediator(loading));
					facade.registerMediator(new DataMediator(factory));
					
					break;
				case App.ASSET_LOAD_DONE:
					setupDeviceSize();
					registerProxy();
					setupDeviceInfo();
					var loadmax:* = LoaderMax; 
					
					var t:ToolTip = ToolTip.getInstance();
					var backShaded:BackShaded			= new BackShaded();
					var game:Game						= new Game();
					var humanRun:HumanRun				= new HumanRun();
					//var admod:Admob						= new Admob();
					
					stage.addChild(t);	
					stage.addChild(game);	
					game.addChildAt(humanRun, game.numChildren - 2);	
					//stage.addChild(admod);		
						
				
					facade.registerMediator(new BackShadedMediator(backShaded));
					facade.registerMediator(new GameMediator(game));
					facade.registerMediator(new ToolTipMediator(t));
					facade.registerMediator(new HumanRunMediator(humanRun));
					
					if (App.PROFILER) SWFProfiler.init(Main.ROOT);
					
					addAdmod();
					//InterstitialDemo();
					
					//FaceCore.init();
					
				break;
				default: break;
            }
        }
		/**
		 * Android: a1532a5df9191bf
		 * iOS a1532a5e59c4d9c
		 * a152834c2b8cce6
		 */
		
		public function addAdmod():void {
			var admob:Admob = Admob.getInstance();
			
			if(admob.supportDevice){
				admob.setKeys(App.BANNER_KEY);
				admob.showBanner(Admob.BANNER,AdmobPosition.TOP_CENTER);
			}
			
		}
		
		protected function setupDeviceSize():void 
		{
			//App.deviceSize = new Rectangle(0, 0,
			//Math.max(stage.fullScreenWidth, stage.fullScreenHeight),
			//Math.min(stage.fullScreenWidth, stage.fullScreenHeight));
		}
        protected function get stage():Stage { 
			return viewComponent as Stage; 
		}
		
		private function setupDeviceInfo():void 
		{
			
			if (SystemProperties.isAndroid() == true) {
				App.BANNER_KEY = "a1532a5df9191bf";
			}else if (SystemProperties.isIOS()== true) {
				App.BANNER_KEY = "a1532a5e59c4d9c";
			}
			//App.BANNER_KEY = "a1532a5e59c4d9c";
		}
		
	
    }
}